package com.yinhai.message.manage.msgmg.bpo.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yinhai.core.common.api.base.IPage;
import com.yinhai.core.common.api.config.impl.SysConfig;
import com.yinhai.core.common.api.util.ValidateUtil;
import com.yinhai.message.manage.msgmg.bpo.IMessageMgBpo;
import com.yinhai.message.manage.msgmg.dao.IMessageMgDao;
import com.yinhai.message.manage.msgmg.vo.MessageDetailVo;
import com.yinhai.message.manage.msgmg.vo.MessageFileVo;
import com.yinhai.message.manage.msgmg.vo.MessageVo;
import com.yinhai.modules.org.api.util.IOrgConstants;

/**
 * @author aolei 2017年3月28日17:15:46
 *         <p>
 *         消息管理实现类
 */
public class MessageMgBpoImpl implements IMessageMgBpo {
    private IMessageMgDao messageMgDao;

    public void setMessageMgDao(IMessageMgDao messageMgDao) {
        this.messageMgDao = messageMgDao;
    }

    @Override
    public IPage<MessageVo> queryMessagesByParam(String sendUserId, String sendSyspathId, String title, Date startTime, Date endTime, int start, int limit, String gridId) {
        Map<String, Object> param = new HashMap<>();
        StringBuffer hql = new StringBuffer();
        hql.append("select new com.yinhai.message.manage.msgmg.vo.MessageVo(")
                .append("mp.msgid,")
                .append("mp.msgdate,")
                .append("mp.title,")
                .append("mp.senduserid,")
                .append("mp.sendsystemid,")
//                .append("u.name,")
                .append("mp.sname,")
                .append("map.systemname")
                .append(") from MessagePo mp ,")
//                .append(SysConfig.getSysConfig("com.yinhai.modules.org.ta3.domain.po.impl.UserPo", "com.yinhai.modules.org.ta3.domain.po.impl.UserPo"))
                .append(" com.yinhai.message.core.po.MessageAppManagePo map")
                .append(" where  mp.sendsystemid = map.systemid");

        if (ValidateUtil.isNotEmpty(sendUserId)) {
            hql.append(" and mp.senduserid =:senduserid");
            param.put("senduserid", sendUserId);
        }
        if (ValidateUtil.isNotEmpty(sendSyspathId)) {
            hql.append(" and mp.sendsystemid =:sendsystemid");
            param.put("sendsystemid", sendSyspathId);
        }
        if (ValidateUtil.isNotEmpty(title)) {
            hql.append(" and mp.title like:title");
            param.put("title", "%"+title+"%");
        }
        if (null != startTime) {
            hql.append(" and mp.msgdate >=:startdate");
            param.put("startdate", startTime);
        }
        if (null != endTime) {
            hql.append(" and mp.msgdate <=:enddate");
            param.put("enddate", endTime);
        }
        return messageMgDao.queryMessagesByParam(hql.toString(), param, start, limit, gridId);
    }

    @Override
    public IPage<MessageDetailVo> queryMessageDetailsByMsgId(String messageId, int start, int limit, String gridId) {
        Map param = new HashMap();
        param.put("messageId", messageId);
        StringBuffer hql = new StringBuffer();
        hql.append("select new com.yinhai.message.manage.msgmg.vo.MessageDetailVo(")
                .append("mdp.id.mgid,")
                .append("mdp.id.userid,")
                .append("mdp.id.systemid,")
                .append("mdp.id.sendertype,")
                .append("mdp.senddate,")
                .append("mdp.errorinfo,")
                .append("mdp.state,")
                .append("mdp.read,")
                .append("mdp.showtype,")
                .append("'',")
                .append("map.systemname")
                .append(") from MessageDetailPo mdp ,")
//                .append(SysConfig.getSysConfig("com.yinhai.modules.org.ta3.domain.po.impl.UserPo", "com.yinhai.modules.org.ta3.domain.po.impl.UserPo"))
                .append("  MessageAppManagePo map")
                .append(" where mdp.id.systemid = map.systemid").append(" and mdp.id.mgid  =:messageId");
        return messageMgDao.queryMessageDetailsByMsgId(hql.toString(), param, start, limit, gridId);
    }

    @Override
    public String queryMessageContextByMessageId(String messageId) {
        StringBuffer hql = new StringBuffer();
        hql.append("select mp.msgcontent from MessagePo mp ")
                .append(" where 1=1 and ")
                .append("mp.msgid=?");
        return messageMgDao.queryMessageContextByMessageId(hql.toString(), messageId);
    }

    @Override
    public List<MessageDetailVo> getAlllUserInfo() {
        StringBuffer hql = new StringBuffer();
        hql.append("select new com.yinhai.message.manage.msgmg.vo.UserNamePassUserVo(")
                .append("u.userid,")
                .append("u.name")
                .append(") from ")
                .append(SysConfig.getSysConfig("com.yinhai.modules.org.ta3.domain.po.impl.UserPo", "com.yinhai.modules.org.ta3.domain.po.impl.UserPo"))
                .append("  u")
                .append(" where 1=1 and  (u.destory is null or u.destory=?) and u.effective=?");
        return messageMgDao.getAlllUserInfo(hql.toString(), IOrgConstants.COMMON_DESTORY_NO,IOrgConstants.EFFECTIVE_YES);
    }

    @Override
    public List<MessageFileVo> queryMessageFilesByMessageId(String messageId) {
        StringBuffer hql = new StringBuffer();
        hql.append("select new com.yinhai.message.manage.msgmg.vo.MessageFileVo(")
                .append("mdfp.filename,")
                .append("mdfp.filepath,")
                .append("mdfp.uploadsuccess,")
                .append("mdfp.filestoremode")
                .append(") from ")
                .append(" com.yinhai.message.core.po.MessageFilePo mdfp")
                .append(" where 1=1 and mdfp.msgid =?");
        return messageMgDao.queryMessageFilesByMessageId(hql.toString(), messageId);

    }
}
